package com.talent.web;

import com.talent.common.Const;
import com.talent.pojo.User;
import com.talent.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by czq on 2018/5/14.
 */
@Controller
@RequestMapping("/login")
public class LoginController extends BaseController{

    @Autowired
    UserService userService;


    @RequestMapping("/loginUI")
    public String loginUI(){
        return "loginUI";
    }

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    public Object Login(String name, String password, HttpSession session){
        try {

            User user = userService.login(name, password);
            if(user != null){
                session.setAttribute(Const.CURRENT_USER, user);
                return renderSuccess("登陆成功");
            }else{
                return renderError("用户不存在或密码错误");
            }
        }catch (Exception e){
            e.printStackTrace();
            return renderError("系统异常");
        }
    }


    /**
     * 退出
     * @param session
     * @return
     */
    @RequestMapping(value = "/logout",method = RequestMethod.POST)
    @ResponseBody
    public Object logout(HttpSession session){
        session.removeAttribute(Const.CURRENT_USER);
        return renderSuccess("退出成功!");
    }
}
